<template>
  <div id="suggestorder">
    <el-button size="mini" type="text" @click="to">回访</el-button>
    <el-dialog :append-to-body="true" :visible.sync="dialog" :title="'回访'" width="940px">
      <el-form ref="form" :model="form" :inline="true" :rules="rules" size="small" label-width="125px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否到场">
              <el-select v-model="form.present" clearable class="filter-item" style="width: 300px;">
                <el-option v-for="item in presentOptions" :key="item.key" :label="item.display_name" :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到场时间">
              <el-date-picker
                v-model="form.presentTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间"
                style="width: 300px;"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-show="!checkRevisitVisible(status)" :loading="loading" type="primary" @click="cancel">返回</el-button>
        <el-button v-show="checkRevisitVisible(status)" type="text" @click="cancel">取消</el-button>
        <el-button v-show="checkRevisitVisible(status)" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, edit } from '@/api/tCustomerSuggest'
import request from '@/utils/request'
import { parseTime } from '@/utils/index'
import { checkRevisitVisible } from '@/api/data'
export default {
  name: 'SuggestOrder',
  props: {
    data: {
      type: Object,
      required: true
    },
    supThis: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      status: 700,
      presentOptions: [
        { key: 1, display_name: '是' },
        { key: 0, display_name: '否' }
      ],
      satisfactionOptions: [
        { key: '0', display_name: '不满意' },
        { key: '1', display_name: '基本满意' },
        { key: '2', display_name: '满意' }
      ],
      loading: false, dialog: false,
      form: {
        id: '',
        orderCode: '',
        present: '',
        presentTime: '',
        satisfaction: '',
        serviceTime: '',
        requireTool: '',
        content: '',
        suggest: '',
        inputOperator: '',
        inputTime: '',
        startTime: ''
      },
      rules: {
      }
    }
  },
  methods: {
    checkRevisitVisible,
    cancel() {
      this.resetForm()
    },
    init() {
      const params = {
        orderCode: this.data.orderCode
      }
      request({
        url: 'api/tCustomerSuggest',
        method: 'get',
        params
      }).then(res => {
        const content = [].concat(res.content)
        const _this = this
        content.forEach(function(data, index) {
          if (data != null) {
            _this.form.id = data.id
            _this.form.orderCode = data.orderCode
            _this.form.present = data.present
            _this.form.presentTime = new Date(data.presentTime)
            _this.form.satisfaction = data.satisfaction
            _this.form.serviceTime = data.serviceTime
            _this.form.requireTool = data.requireTool
            _this.form.content = data.content
            _this.form.suggest = data.suggest
            return
          }
        })
      }).catch(err => {
        console.log(err)
      })
    },
    to() {
      this.status = this.data.status
      console.log(this.status)
      this.form.orderCode = this.data.orderCode
      this.init()
      this.dialog = true
    },
    doSubmit() {
      this.loading = true
      if (this.form.id === '' || this.form.id === null) {
        this.doAdd()
      } else {
        this.form.presentTime = parseTime(this.form.presentTime)
        this.doEdit()
      }
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.supThis.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        orderCode: '',
        present: '',
        presentTime: '',
        satisfaction: '',
        serviceTime: '',
        requireTool: '',
        content: '',
        suggest: '',
        inputOperator: '',
        inputTime: '',
        startTime: ''
      }
    }
  }
}
</script>

<style scoped>
 #suggestorder{
    display: inline-block;
    margin-right: 3px;
  }
</style>
